// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem501.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=501 lang=cpp
 *
 * [501] 二叉搜索树中的众数
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    

    void findMode(TreeNode* root,unordered_map<int,int> &tree,int &max)
    {
        if(root == NULL)
            return ;
        tree[root->val]++;
        if(tree[root->val]  > max)
        {
            max = tree[root->val];
        }
        findMode(root->left,tree,max);
        findMode(root->right,tree,max);
    }
    vector<int> findMode(TreeNode* root) {
        vector<int> res;
        if(root == NULL)
        {
            return res;
        }
        unordered_map<int,int> tree;

        int max = 0;
        findMode(root,tree,max);

        for(auto it = tree.begin();it!=tree.end();it++)
        {
            if(it->second == max)
                res.push_back(it->first);
        }
        return res;
    }
};
// @lc code=end

